package com.seckill.service.impl;

import com.github.pagehelper.PageHelper;
import com.seckill.mapper.SeckillActivityMapper;
import com.seckill.pojo.dto.ActivityPageQueryDTO;
import com.seckill.pojo.entity.SeckillActivity;
import com.seckill.service.SeckillActivityService;
import com.sky.context.BaseContext;

import com.sky.result.PageResult;
import com.sky.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SeckillActivityServiceImpl implements SeckillActivityService {
    @Autowired
    private SeckillActivityMapper seckillActivityMapper;
    @Override
    public Boolean save(SeckillActivity seckillActivity) {
        Integer row = seckillActivityMapper.insert(seckillActivity);
        if(row<1){
            return false;
        }
        return true;
    }

    @Override
    public PageResult pageQuery(ActivityPageQueryDTO activityPageQueryDTO) {
        PageHelper.startPage(activityPageQueryDTO.getPage(),activityPageQueryDTO.getPageSize());
        List<SeckillActivity> seckillActivities = seckillActivityMapper.pageQuery(activityPageQueryDTO.getName());
        PageResult pageResult = new PageResult();
        if(seckillActivities==null){
            List<SeckillActivity> seckillActivityList = new ArrayList<>();
            pageResult.setRecords(seckillActivityList);
            return pageResult;
        }
        pageResult.setRecords(seckillActivities);
        return pageResult;
    }

    /**
     * 查询根据id查询活动信息
     * @param id
     * @return
     */
    @Override
    public SeckillActivity getById(Long id) {
        SeckillActivity seckillActivity = seckillActivityMapper.getById(id);
        if (seckillActivity == null) { return new SeckillActivity(); }
        return seckillActivity;
    }

    /**
     * 更新活动信息
     * @param seckillActivity
     * @return
     */
    @Override
    public Boolean update(SeckillActivity seckillActivity) {
        seckillActivity.setCreateUser(BaseContext.getCurrentId());
        Integer update = seckillActivityMapper.update(seckillActivity);
        if(update<1){
            return false;
        }
        return true;
    }

    @Override
    public Boolean delete(Long id) {
        Integer delete = seckillActivityMapper.delete(id);
        if(delete<1){
            return false;
        }
        return true;
    }

    @Override
    public void startActivity(Long id) {

    }

    @Override
    public void endActivity(Long id) {

    }
}
